package com.cpi.cornpp.common.files;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.common.files.dto.SAPComponentDTO;
import com.cpi.cornpp.common.files.dto.SAPComponentInternalOrderDTO;
import com.cpi.cornpp.common.files.exception.SAPComponentFileException;
import com.cs.common.pool.exception.PoolException;
import com.cs.common.utils.NumberUtil;
import com.cs.common.utils.StringUtils;

public class SAPComponentInternalOrder extends SAPComponentFile {
	private static final String CHAR_DELIMITER = "|";//$NON-NLS-1$
	private static final String SQL_INSERT_INTERNAL_ORDER = "INSERT INTO [sap_internal_order] ([code] ,[date] ,[order_id] ,[description] ,[amount]) VALUES (?, ?, ?, ?, ?)";//$NON-NLS-1$
	private static final String DATE_FORMAT = "dd.MM.yyyy";//$NON-NLS-1$
	private static final int OFF_SET_INIT = 3;
	private static final String CHAR_DASH= "-";//$NON-NLS-1$
	public SAPComponentInternalOrder(String fileName) {
		super(fileName);
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#parse(java.util.List)
	 */
	public List parse(List list) throws SAPComponentFileException {
		List sapComponentInternalOrderDTOs = null;
		Iterator iterator = null;
		String line = null;
		SAPComponentInternalOrderDTO sapComponentInternalOrderDTO = null;
		StringTokenizer stringTokenizer = null;
		String token = null;
		Calendar date = null;
		SimpleDateFormat simpleDateFormat = null;
		int totalReg = 0;
		try{
			simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
			iterator = list.iterator();
			totalReg = list.size();
			sapComponentInternalOrderDTOs = new ArrayList(0);
			for( int mainIndex = 0; mainIndex < totalReg ; mainIndex++ ){
				line = (String)iterator.next();
				if( mainIndex+1 > OFF_SET_INIT ){
					stringTokenizer = new StringTokenizer(line, CHAR_DELIMITER, false);
					sapComponentInternalOrderDTO = new SAPComponentInternalOrderDTO();
					for(int index = 0;stringTokenizer.hasMoreTokens();index++){
						token = stringTokenizer.nextToken();
						switch(index){
							case 0:	sapComponentInternalOrderDTO.setCode(token);
										break;
							case 1:	date = Calendar.getInstance();
										date.setTime(simpleDateFormat.parse(token));
										sapComponentInternalOrderDTO.setDate(date);
										break;
							case 2:	sapComponentInternalOrderDTO.setOrderId(token);
										break;
							case 3:  sapComponentInternalOrderDTO.setDescription(token);
										break;
							case 4 :	sapComponentInternalOrderDTO.setAmount(NumberUtil.parseDouble(token.replaceAll(CHAR_DASH, StringUtils.EMPTY_STRING)));
										break;
						}
					}
					sapComponentInternalOrderDTOs.add(sapComponentInternalOrderDTO);
				}
			}
		}catch(Exception exception){
			SAPComponentFileException sapComponentFileException = null;
			sapComponentFileException = new SAPComponentFileException(exception, CornPPException.LAYER_SERVICE, CornPPException.ACTION_LISTS);
			throw sapComponentFileException;
		}
		return sapComponentInternalOrderDTOs;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#insertAll(java.util.List)
	 */
	public int insertAll(List list) throws SAPComponentFileException {
		int totalInserted = 0;
		Iterator iterator = null;
		SAPComponentInternalOrderDTO sapComponentInternalOrderDTO = null;
		try{
			iterator = list.iterator();
			while(iterator.hasNext()){
				sapComponentInternalOrderDTO = (SAPComponentInternalOrderDTO)iterator.next();
				try{
					this.insert(sapComponentInternalOrderDTO);
					totalInserted++;
				}catch(SAPComponentFileException sapComponentFileException){
					System.out.println("Error DTO ["+sapComponentFileException.getMessage()+"] : " + sapComponentInternalOrderDTO);
				}
			}
		} catch(Exception exception){
			SAPComponentFileException sapComponentFileException = null;
			exception.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (exception, CornPPException.LAYER_DAO,CornPPException.ACTION_SELECT);
			throw sapComponentFileException;
		}
		return totalInserted;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#insert(com.cpi.cornpp.common.files.dto.SAPComponentDTO)
	 */
	public void insert(SAPComponentDTO sapComponentDTO) throws SAPComponentFileException {
		SAPComponentInternalOrderDTO sapComponentInternalOrderDTO = null;
		PreparedStatement preparedStatement = null;
		Date sqlDate = null;
		Calendar calendar = null;
		try{
			sapComponentInternalOrderDTO = (SAPComponentInternalOrderDTO)sapComponentDTO;
			calendar = sapComponentInternalOrderDTO.getDate();
			sqlDate = new Date(calendar.getTimeInMillis());
			preparedStatement = prepareSQL(SQL_INSERT_INTERNAL_ORDER, dsName);
			preparedStatement.setString(1, sapComponentInternalOrderDTO.getCode());
			preparedStatement.setDate(2, sqlDate);
			preparedStatement.setString(3, sapComponentInternalOrderDTO.getOrderId());
			preparedStatement.setString(4, sapComponentInternalOrderDTO.getDescription());
			preparedStatement.setDouble(5, sapComponentInternalOrderDTO.getAmount());
			executeInsert(preparedStatement);
		}catch(PoolException poolException){
			SAPComponentFileException sapComponentFileException = null;
			poolException.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (poolException, CornPPException.LAYER_DAO,CornPPException.ACTION_INSERT);
			throw sapComponentFileException;
		}catch(SQLException sqlException){
			SAPComponentFileException sapComponentFileException = null;
			sqlException.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (sqlException, CornPPException.LAYER_DAO,CornPPException.ACTION_INSERT);
			throw sapComponentFileException;
		}finally{
			try {
				closeConnection(preparedStatement.getConnection());
			} catch (SQLException sqlException) {}
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#size()
	 */
	public int size() throws SAPComponentFileException {
		int size = 0;
		List records = null;
		try{
			records = getContent();
			size = records.size() - OFF_SET_INIT;
		}finally{
			records = null;
		}
		return size;
	}
}